package com.example.demo.jpa;

import com.example.demo.jpa.customer.domain.Customer;
import com.example.demo.jpa.customer.dto.CustomerDTO;
import com.example.demo.jpa.customer.repo.CustomerPageRepository;
import com.example.demo.jpa.customer.repo.CustomerRepository;
import com.example.demo.jpa.product.domain.FinancialProduct;
import com.example.demo.jpa.product.repo.FinancialProductRepository;
import com.example.demo.jpa.org.domain.Org;
import com.example.demo.jpa.org.repo.OrgRepository;
import com.example.demo.jpa.person.domain.Person;
import com.example.demo.jpa.person.repo.PersonRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.scheduling.annotation.EnableScheduling;
import uk.co.jemos.podam.api.PodamFactory;
import uk.co.jemos.podam.api.PodamFactoryImpl;

@SpringBootApplication
public class JpaApplication {

	private static final Logger log = LoggerFactory.getLogger(JpaApplication.class);


	public static void main(String[] args) {
		SpringApplication.run(JpaApplication.class, args);
	}


	@Bean
	public CommandLineRunner productdemo(FinancialProductRepository repository ) {
		return (args) -> {
			// save a few customers
			repository.save(new FinancialProduct("杭银理财第一期", 2.1, 2.3, "高", "杭州银行"));
			repository.save(new FinancialProduct("杭银理财第四期", 1.3, 1.4, "低", "杭州银行"));

			// fetch all customers
			log.info("Products found with findAll():");
			log.info("-------------------------------");
			repository.findAll().forEach(product -> {
				log.info(product.toString());
			});
			log.info("");

			// fetch an individual customer by ID
			FinancialProduct financialProduct = repository.findById(1L);
			log.info("products found with findById(1L):");
			log.info("--------------------------------");
			log.info(financialProduct.toString());
			log.info("");
		};
	}

	@Bean
	public PodamFactory podamFactory(){
		return new PodamFactoryImpl();
	}
	@Bean
	CommandLineRunner runner(PersonRepository repository) {
		return args -> {
                Person person = new Person();
                person.setName("John");
                repository.save(person);
                Person saved = repository.findById(person.getId()).orElseThrow(RuntimeException::new);
            };
        }

	@Bean
    public CommandLineRunner demo(CustomerRepository repository, CustomerPageRepository pageRepository) {
       return (args) -> {
                // save a few customers
            repository.save(new Customer("Jack", "Bauer",0));
            repository.save(new Customer("Chloe", "O'Brian",1));
            repository.save(new Customer("Kim", "Bauer",0));
            repository.save(new Customer("David", "Palmer",1));
            repository.save(new Customer("Michelle", "Dessler",1));

                // fetch all customers
                log.info("Customers found with findAll():");
                log.info("-------------------------------");
                repository.findAll().forEach(customer -> {
                    log.info(customer.toString());
                });
                log.info("");

                // fetch an individual customer by ID
                Customer customer = repository.findById(1L);
                log.info("Customer found with findById(1L):");
                log.info("--------------------------------");
                log.info(customer.toString());
                log.info("");

                // fetch customers by last name
                log.info("Customer found with findByLastName('Bauer'):");
                log.info("--------------------------------------------");
                repository.findByLastName("Bauer").forEach(bauer -> {
                    log.info(bauer.toString());
                });
                log.info("");

                Customer update=repository.findById(3);
                update.setLastName("sdaa");
                repository.save(update);
                repository.findAll().forEach(it -> {
                    log.info(it.toString());
                });

                Page<Customer> page = pageRepository.findAll(PageRequest.of(0,3));
                Page<CustomerDTO> pageD=page.map(customer1 -> new CustomerDTO(customer1.getId(), customer1.getFirstName(), customer1.getLastName()));
                pageD.forEach((it -> {
                    log.info(it.toString());
                }));
		};
	}
    @Bean
	public CommandLineRunner orgDemo(OrgRepository repository1, OrgRepository pageRepository) {
		return (args) -> {
			// save a few customers
			repository1.save(new Org("杭州银行合肥分行营业部", "包河区徽州大道4872号"));
			repository1.save(new Org("杭州银行合肥包河支行", "包河区繁华大道6699号"));
			repository1.save(new Org("杭州银行合肥瑶海支行", "瑶海区芜湖路一号"));
			repository1.save(new Org("杭州银行合肥庐阳支行", "庐阳区北一环路279号"));
			repository1.save(new Org("杭州银行合肥庐江支行", "庐江县丝绸路1105号"));

			// fetch all customers
			log.info("org found with findAll():");
			log.info("------------------------1");
			repository1.findAll().forEach(org -> {
				log.info(org.toString());
			});
			log.info("");

			// fetch an individual customer by ID
			Org org = repository1.findById(1L);
			log.info("Customer found with findById(1L) :");
			log.info("--------------------------------2");
			log.info(org.toString());
			log.info("");


			Org update=repository1.findById(3);
			update.setName("杭州银行长丰支行");
			repository1.save(update);
			repository1.findAll().forEach(it -> {
				log.info(it.toString());
			});

		};
	}




}